package com.pirestupppai.utils;

import android.content.Context;
import android.content.Intent;

import com.pirestupppai.bean.LoginUser;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.ui.activity.MainActivity;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformDb;

/**
 * 第三方登录工具类
 *
 * @author: xiaoluo
 * @date: 2017-03-15 17:51
 */
public class OauthUtil {
    public static final String PLATFORM_WECHAT = "wechat-open";
    public static final String PLATFORM_QQ = "qq-android";
    public static final String PLATFORM_WEIBO = "weibo";

    /**
     * 授权登录
     */
    public static void oauthLogin(final Context context, Platform platform, String platformName) {

        PlatformDb platformDb = platform.getDb();
        String accessToken = platformDb.getToken(); // 获取授权token
        String openId = platformDb.getUserId();    // 获取用户在此平台的ID

        NetworkRequest.getInstance().oauthLogin(context, accessToken, openId, platformName,
                new TupaiSubscriber<TupaiUser>(context) {
                    @Override
                    protected void requestSuccess(TupaiUser tupaiUser) {
                        LoginUser.getInstance().initFromResponse(tupaiUser);
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(intent);
                    }

                    @Override
                    protected void requestError(String error) {

                    }
                });
    }
}
